package com.wf.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.constant.RedisConstant;
import com.wf.context.BaseContext;
import com.wf.domain.RepairInfo;
import com.wf.service.RepairInfoService;
import com.wf.utils.RedisUtil;
import com.wf.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 个人报修
 *
 * @author mpk
 * @since 2024/8/30 18:38:39
 */
@RestController
@RequestMapping("/app/repair")
@Slf4j
public class RepairController {

    @Autowired
    RepairInfoService repairInfoService;

    @Autowired
    RedisUtil redisUtil;

    /**
     * 个人报修提交
     */
    @PostMapping
    public Result insertRepair(@RequestBody RepairInfo repairInfo) {

        List<String> imageUrls = repairInfo.getImage(); // 更具描述性的变量名

        imageUrls.forEach(
                imageUrl -> {
                    String processedUrl = imageUrl.replace("https://wfyn.oss-cn-hangzhou.aliyuncs.com/", ""); // 显式存储替换结果
                    try {
                        redisUtil.sSet(RedisConstant.APARTMENT_USED_IMG, processedUrl); // 尝试设置值
                    } catch (Exception e) {
                        // 异常处理
                        System.err.println("Error setting value in Redis: " + e.getMessage());
                    }
                }
        );


        return repairInfoService.insertRepair(repairInfo) ? Result.success("维修上报提交成功！", "") : Result.failure("提交失败！");
    }

    /**
     * 查看个人报修
     */
    @GetMapping("/get")
    public Result getRepair() {

        LambdaQueryWrapper<RepairInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RepairInfo::getUserId, BaseContext.getCurrentId());
        List<RepairInfo> list = repairInfoService.list(wrapper);

        return Result.success(list);
    }

}
